package com.example.testtouch2;

import javax.microedition.khronos.opengles.GL10;

import com.demo.androidgame.fx.gl.Animation;
import com.demo.androidgame.fx.gl.Camera2D;
import com.demo.androidgame.fx.gl.SpriteBatcher;
import com.demo.androidgame.fx.gl.TextureRegion;
import com.demo.androidgame.fx.impl.GLGraphics;

public class WorldRenderer {
    static final float FRUSTUM_WIDTH = 720;
    static final float FRUSTUM_HEIGHT =1280;
//    Class<TestScreen> level1;
//    Class<Level2Screen> level2;
//    Class<Level3Screen> level3;
//    Class<Level4Screen> level4;
    GLGraphics glGraphics;
    World world;
    Camera2D cam;
    SpriteBatcher batcher;    
    int worldLevel;
    static boolean nfcCheck = false;
    
    public WorldRenderer(GLGraphics glGraphics, SpriteBatcher batcher, World world, int WorldLevel) {
        this.glGraphics = glGraphics;
        this.world = world;
        this.cam = new Camera2D(glGraphics, FRUSTUM_WIDTH, FRUSTUM_HEIGHT);
        this.batcher = batcher;        
        this.worldLevel = WorldLevel;
    }
    
    public void render() {
    	
        cam.setViewportAndMatrices();
        
        renderBackground();
        
        renderObjects(); 
        
    }
    


	public void renderBackground() {
    	
        switch (worldLevel) {
		case 1:
			batcher.beginBatch(Assets.background);
        	batcher.drawSprite(cam.position.x, cam.position.y, FRUSTUM_WIDTH, FRUSTUM_HEIGHT, Assets.backgroundRegion);	
        	batcher.endBatch();
			break;
		case 2:
			batcher.beginBatch(Assets.background2);
        	batcher.drawSprite(cam.position.x, cam.position.y, FRUSTUM_WIDTH, FRUSTUM_HEIGHT, Assets.backgroundRegion2);	
        	batcher.endBatch();
			break;
		case 3:
			batcher.beginBatch(Assets.background3);
        	batcher.drawSprite(cam.position.x, cam.position.y, FRUSTUM_WIDTH, FRUSTUM_HEIGHT, Assets.backgroundRegion3);	
        	batcher.endBatch();
			break;
		case 4:
			batcher.beginBatch(Assets.background4);
        	batcher.drawSprite(cam.position.x, cam.position.y, FRUSTUM_WIDTH, FRUSTUM_HEIGHT, Assets.backgroundRegion4);	
        	batcher.endBatch();
			break;
		}
        
		
//        if(world.gameLevel ==1){
//        }
//        else if(world.gameLevel ==2){
//        }
//        else if(world.gameLevel ==3){
//        }
//        else if(world.gameLevel ==4){
//        }
        
    }
    
    public void renderObjects() {
        GL10 gl = glGraphics.getGL();
        gl.glEnable(GL10.GL_BLEND);
        gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
//        renderTimer();
        renderEnemy1();
        renderCamel();
        renderCoin();
        renderPresent();
        renderPlayer();
        gl.glDisable(GL10.GL_BLEND);
    }
   
        
    private void renderPlayer() {

    	batcher.beginBatch(Assets.atlas);
        float sidey = world.player.velocity.y < 0? -1: 1;
       batcher.drawSprite(world.player.position.x, world.player.position.y, 72,sidey * 72, Assets.playerRegion);
       batcher.endBatch();
    }

    private void renderEnemy1(){
    	if(world.gameLevel == 1){
   	 TextureRegion keyFrame;
   	 keyFrame = Assets.enemy1.getKeyFrame(world.enemy1.stateTime, Animation.ANIMATION_LOOPING);
   	batcher.beginBatch(Assets.atlas);
   	batcher.drawSprite(world.enemy1.position.x, world.enemy1.position.y,2 * World.X_BOX, World.Y_BOX, keyFrame);
   	batcher.endBatch();
    	}
    	
    	}
    
    private void renderCamel(){
    	if(world.gameLevel == 2){
      	 TextureRegion keyFrame;
      	 keyFrame = Assets.camel.getKeyFrame(world.camel.stateTime, Animation.ANIMATION_LOOPING);
      	batcher.beginBatch(Assets.atlas);
      	batcher.drawSprite(world.camel.position.x, world.camel.position.y,2 * World.X_BOX, World.Y_BOX, keyFrame);
      	batcher.drawSprite(world.camel2.position.x, world.camel2.position.y,2 * World.X_BOX, World.Y_BOX, keyFrame);
      	batcher.endBatch();}
      	 
       	
       	}

    private void renderPresent() {
//		 선물 batcher
   
    	batcher.beginBatch(Assets.present);
        batcher.drawSprite(world.present1s.position.x, world.present1s.position.y, 72, 72, Assets.present1);
        batcher.drawSprite(world.present2s.position.x, world.present2s.position.y, 72, 72, Assets.present2);
        batcher.drawSprite(world.present3s.position.x, world.present3s.position.y, 72, 72, Assets.present3);
        batcher.drawSprite(world.present4s.position.x, world.present4s.position.y, 72, 72, Assets.present4);
        batcher.endBatch();
  
    	
//			
//			if(TestScreen.level1 = true){
//		
//   
//   				int leng = world.present1s.size();
//   				  for(int j = 0; j < leng ; j++) {
//   				Present present = world.present1s.get(j);	
//   			  GL10 gl = glGraphics.getGL();
//   	        gl.glEnable(GL10.GL_BLEND);
//   	        gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
//		 batcher.beginBatch(Assets.present);			
// 		 batcher.drawSprite(world.present1s.get(0).position.x, world.present1s.get(0).position.y, World.X_BOX, World.Y_BOX, Assets.present1);
//		 batcher.drawSprite(world.present1s.get(1).position.x, world.present1s.get(1).position.y, World.X_BOX, World.Y_BOX, Assets.present2);
//		 batcher.drawSprite(world.present1s.get(2).position.x, world.present1s.get(2).position.y, World.X_BOX, World.Y_BOX, Assets.present3);
//		 batcher.drawSprite(world.present1s.get(3).position.x, world.present1s.get(3).position.y, World.X_BOX, World.Y_BOX, Assets.present4);
//		 batcher.endBatch();
//	        gl.glDisable(GL10.GL_BLEND);
//
// 				}
// 				}
// 	
// 	  				
// 				else if(Level2Screen.level2 == true){  
// 	  				
// 	  			  	int leng2 = world.present2s.size();
// 	    	  		for(int j = 0; j < leng2 ; j++) {
// 	    	  			Present2 present2 = world.present2s.get(j);
// 	    	  		 GL10 gl = glGraphics.getGL();
// 	       	        gl.glEnable(GL10.GL_BLEND);
// 	       	        gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
// 	  		     batcher.beginBatch(Assets.present);			
// 	  			 batcher.drawSprite(world.present2s.get(0).position.x, world.present2s.get(0).position.y, World.X_BOX, World.Y_BOX, Assets.present1);
// 	  			 batcher.drawSprite(world.present2s.get(1).position.x, world.present2s.get(1).position.y, World.X_BOX, World.Y_BOX, Assets.present2);
// 	  			 batcher.drawSprite(world.present2s.get(2).position.x, world.present2s.get(2).position.y, World.X_BOX, World.Y_BOX, Assets.present3);
// 	  			 batcher.drawSprite(world.present2s.get(3).position.x, world.present2s.get(3).position.y, World.X_BOX, World.Y_BOX, Assets.present4);
// 	  			 batcher.endBatch();
// 		        gl.glDisable(GL10.GL_BLEND);
//
// 	    	  		}
// 	  		}
//// 	  
////	  				
// 				else if(Level3Screen.level3 == true){  
//	  					int leng3 = world.present3s.size();
//	  			  		for(int j = 0; j < leng3 ; j++) {
//	  			  			Present3 present3 = world.present3s.get(j);
//	  			  		 GL10 gl = glGraphics.getGL();
//	  	       	        gl.glEnable(GL10.GL_BLEND);
//	  	       	        gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA); 			
//	  	 		 batcher.beginBatch(Assets.present);		  			
//	  			 batcher.drawSprite(world.present3s.get(0).position.x, world.present3s.get(0).position.y, World.X_BOX, World.Y_BOX, Assets.present1);
//	  			 batcher.drawSprite(world.present3s.get(1).position.x, world.present3s.get(1).position.y, World.X_BOX, World.Y_BOX, Assets.present2);
//	  			 batcher.drawSprite(world.present3s.get(2).position.x, world.present3s.get(2).position.y, World.X_BOX, World.Y_BOX, Assets.present3);
//	  			 batcher.drawSprite(world.present3s.get(3).position.x, world.present3s.get(3).position.y, World.X_BOX, World.Y_BOX, Assets.present4);
//	  			 batcher.endBatch();	
//	  		     gl.glDisable(GL10.GL_BLEND);
//	  			  		}
//	  		}
//	  		
// 				else if(Level4Screen.level4 == true){ 
// 	  		  		int leng4 = world.present4s.size();
// 	  	  	  		for(int j = 0; j < leng4 ; j++) {
// 	  	  	  			Present4 present4 = world.present4s.get(j);
// 	  	  	  	 batcher.beginBatch(Assets.present);		
// 	  			 batcher.drawSprite(world.present4s.get(0).position.x, world.present4s.get(0).position.y, World.X_BOX, World.Y_BOX, Assets.present1);
// 	  			 batcher.drawSprite(world.present4s.get(1).position.x, world.present4s.get(1).position.y, World.X_BOX, World.Y_BOX, Assets.present2);
// 	  			 batcher.drawSprite(world.present4s.get(2).position.x, world.present4s.get(2).position.y, World.X_BOX, World.Y_BOX, Assets.present3);
// 	  			 batcher.drawSprite(world.present4s.get(3).position.x, world.present4s.get(3).position.y, World.X_BOX, World.Y_BOX, Assets.present4);
// 	  			 batcher.endBatch();
// 	  				}
// 	  		}
   }
   private void renderCoin(){
	    //nfc
	   if(nfcCheck == true){
//	    GL10 gl2 = glGraphics.getGL();
//        gl2.glEnable(GL10.GL_BLEND);
//        gl2.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
        
 
			batcher.beginBatch(Assets.atlas);
			  
	
				
				int len = world.coins.size();
				  for(int i = 0; i < len ; i++) {
				
				Coin coin = world.coins.get(i);
		TextureRegion keyFrame;
		keyFrame = Assets.coinAnim.getKeyFrame(coin.stateTime, Animation.ANIMATION_LOOPING);
		batcher.drawSprite(coin.position.x, coin.position.y,70, 70, keyFrame);
		batcher.endBatch();
		
			     }

//		if(Level2Screen.level2 == true){
//			int len2 = world.coins2.size();
//			  for(int i = 0; i < len2 ; i++) {
//			
//			Coin2 coin = world.coins2.get(i);
//		
//			TextureRegion keyFrame;
//		keyFrame = Assets.coinAnim.getKeyFrame(coin.stateTime, Animation.ANIMATION_LOOPING);
//		batcher.drawSprite(coin.position.x, coin.position.y,70, 70, keyFrame);}
//		}
//
//		else if(Level3Screen.level3 == true){
//			int len3 = world.coins3.size();
//			  for(int i = 0; i < len3 ; i++) {
//			
//			Coin3 coin = world.coins3.get(i);
//		
//			TextureRegion keyFrame;
//		keyFrame = Assets.coinAnim.getKeyFrame(coin.stateTime, Animation.ANIMATION_LOOPING);
//		batcher.drawSprite(coin.position.x, coin.position.y,70, 70, keyFrame);
//		}
//		}
//
//		else if(Level4Screen.level4 == true){
//			int len4 = world.coins4.size();
//			  for(int i = 0; i < len4 ; i++) {		
//		Coin4 coin = world.coins4.get(i);
//	
//		TextureRegion keyFrame;
//		keyFrame = Assets.coinAnim.getKeyFrame(coin.stateTime, Animation.ANIMATION_LOOPING);
//		batcher.drawSprite(coin.position.x, coin.position.y,70, 70, keyFrame);
//		  }
//		}
		}

//		gl2.glDisable(GL10.GL_BLEND);}
		
	   }
   }

   

